﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cost.Models;
using NBlock.MVCExtensions;
using NBlock.Core;
using Cost.Services;

namespace Cost.Controllers
{
    public class TempDishOrderController : Controller
    {
        #region 构造函数
        private readonly ICostRepository<DishOrder> dishorderRepository;
        private readonly ICostRepository<DishOrderItem> dishorderitemRepository;
        private readonly ICostRepository<DishOrderItemView> dishOrderItemViewRepository;
        private readonly ICostRepository<DishMaterial> dishmaterialRepository;
        private readonly ICostRepository<MaterialOrderItem> materialorderitemRepository;
        private readonly ICostRepository<ProjectSite> projectSiteRepository;
        private readonly OrderService orderService;
        private readonly IEnumService enumService;
        public TempDishOrderController(ICostRepository<DishOrder> dishorderRepository,
            IEnumService enumService,
            OrderService orderService,
            ICostRepository<ReportedOrder> reportedOrderRepository,
            ICostRepository<DishOrderItem> dishorderitemRepository,
            ICostRepository<DishOrderItemView> dishOrderItemViewRepository,
            ICostRepository<MaterialOrderItem> materialorderitemRepository,
            ICostRepository<DishMaterial> dishmaterialRepository,
            ICostRepository<ProjectSite> projectSiteRepository)
        {
            this.dishorderRepository = dishorderRepository;
            this.dishorderitemRepository = dishorderitemRepository;
            this.dishOrderItemViewRepository = dishOrderItemViewRepository;
            this.enumService = enumService;
            this.orderService = orderService;
            this.dishmaterialRepository = dishmaterialRepository;
            this.materialorderitemRepository = materialorderitemRepository;
            this.projectSiteRepository = projectSiteRepository;
        }
        #endregion

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Edit(string Id)
        {
            var data = dishorderRepository.Find(Id) ?? new DishOrder
            {
                OrderDate = DateTime.Today.AddDays(2),
                OrderType = "补充订单",
                State = "草稿",
                SiteId = nbUserContext.Current.CurrentUser.UnitCode,
                SiteName = nbUserContext.Current.CurrentUser.UnitName,
            };
            return View(data);
        }

        [HttpPost]
        public JsonResult List(nbPageInfo pager, nbSearchCondition where)
        {
            var siteId = nbUserContext.Current.CurrentUser.UnitCode;
            var pageData = dishorderRepository.GetPageable(pager, d => d.SiteId == siteId && d.OrderType == "补充订单", where);
            var data = pager.ToGridData(pageData);
            return Json(data);
        }

        [HttpPost]
        public JsonResult Delete(string Id)
        {
            orderService.DeleteDishOrder(Id);
            return Json(nbAjaxMessage.Successful);
        }

        [HttpPost]
        public JsonResult Save(string Id, IList<DishOrderItem> dishList)
        {
            var order = dishorderRepository.Find(Id) ?? new DishOrder();

            // 清空所有的子表信息
            orderService.DeleteDishOrderItem(order.Id);

            // 保存订单信息
            TryUpdateModel(order);
            orderService.SaveDishOrder(order, dishList);

            return Json(nbAjaxMessage.Successful);
        }

        [HttpPost]
        public JsonResult EditList(string OrderId, nbPageInfo pager, nbSearchCondition where)
        {
            var list = dishOrderItemViewRepository.GetPageable(pager, m => m.OrderId == OrderId, where);
            var pageList = list.Page.ToList();
            var fdata = pager.ToGridData(pageList, list.Total);
            return Json(fdata);
        }


        #region 上报订单

        public ActionResult ReportOrder(string Id)
        {
            var data = dishorderRepository.Find(Id);

            if (!data.ReportDate.HasValue)
            {
                data.ReportDate = DateTime.Now;
            }
            if (string.IsNullOrWhiteSpace(data.ReportUserName))
            {
                data.ReportUserId = nbUserContext.Current.CurrentUser.UserId;
                data.ReportUserName = nbUserContext.Current.CurrentUser.UserName;
            }

            return View(data);
        }

        [HttpPost]
        public JsonResult SaveReportOrder(string Id, string password)
        {
            var data = dishorderRepository.Find(Id);

            //// 判断上报密码是否正确
            //var pwd = projectSiteRepository.FindAll(s => s.Id == data.SiteId).Select(s => s.ReportOrderPassword).FirstOrDefault();
            //if (password == pwd)
            //{
            // 更改订单的状态和统计订单的数量
            TryUpdateModel(data);
            orderService.ReportOrder(data);
            return Json(nbAjaxMessage.Successful);
            //}
            //else
            //{
            //    return Json(new nbAjaxMessage { IsSuccess = false, Msg = "上报密码错误，请重新输入的正确密码！" });
            //}

        }
        #endregion
    }
}
